<template>
	<view class="container">
		<view class="formBox">
			<uni-forms ref="form" :modelValue="detail">
				<view class="box">
					<uni-forms-item label="锁ID" name="lock_id">
						<view class="dis-input">{{ detail.lock_id }}</view>
					</uni-forms-item>
					<uni-forms-item label="类型" name="lock_type_name">
						<view class="dis-input">{{ detail.lock_type_name }}</view>
					</uni-forms-item>
					<uni-forms-item label="电量" name="electric_quantity">
						<view class="dis-input">{{ detail.electric_quantity }}%</view>
					</uni-forms-item>
				</view>
				<view class="box">
					<uni-forms-item label="锁名称" name="lock_alias">
						<view class="is-input" @click="setName">
							{{ detail.lock_alias }}
							<uni-icons type="right" size="16" color="#bbb"></uni-icons>
						</view>
					</uni-forms-item>
					<uni-forms-item label="分组" name="group_id">
						<view class="dis-input">{{ detail.group_name }}</view>
					</uni-forms-item>
					<uni-forms-item v-if="detail.bind_room_id != 0" label="绑定房间" name="bind_room_id">
						<view class="dis-input">{{ detail.room_address }}</view>
					</uni-forms-item>
				</view>
				<view class="box">
					<uni-forms-item label="开锁密码" name="no_keypwd">
						<view class="dis-input">{{ detail.no_keypwd }}</view>
					</uni-forms-item>
				</view>
				<view class="btns">
					<button class="btn" type="warn" @click="deleteLock">删除锁</button>
				</view>
			</uni-forms>
			<uni-popup ref="inputDialog" type="dialog">
				<uni-popup-dialog
					ref="inputClose"
					mode="input"
					title="修改锁名称"
					:value="detail.lock_alias"
					placeholder="请输入锁名称"
					@confirm="dialogInputConfirm"
				></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
import api from '@/api/api';
const plugin = requirePlugin('ttLock');
export default {
	data() {
		return {
			id: null,
			detail: {}
		};
	},

	onLoad(e) {
		this.id = e.id;
	},
	onShow() {
		this.getDetail();
	},
	methods: {
		setName() {
			this.$refs.inputDialog.open();
		},
		dialogInputConfirm(val) {
			const data = {
				lock_id: this.detail.lock_id,
				lock_name: val
			};
			uni.showLoading({
				title: '重命名中...'
			});
			api.lockRename(data).then((res) => {
				if (res.data.is_success) {
					uni.hideLoading();
					uni.showToast({
						title: '智能锁重命名成功',
						icon: 'none'
					});
					this.$refs.inputDialog.close();
					this.getDetail();
				}
			});
		},
		deleteLock() {
			const lockData = this.detail.lock_data;
			let deviceId = this.detail.lock_mac;
			const start = Date.now();
			uni.showLoading({
				title: '连接智能锁中...'
			});
			plugin
				.resetLock(lockData, (res) => {}, deviceId)
				.then((res) => {
					if (res.deviceId) {
						deviceId = res.deviceId;
					}
					if (res.errorCode === 0) {
						uni.showLoading({
							title: '同步服务器中...'
						});
						// 同步到服务器
						api.lockDelete({
							lock_ids: this.detail.lock_id
						}).then((res) => {
							if (res.data.is_success) {
								uni.hideLoading();
								uni.showToast({
									title: '智能锁已删除'
								});
								setTimeout(() => {
									uni.navigateBack();
								}, 1000);
							} else {
								uni.showToast({
									icon: 'none',
									title: '同步服务器失败，智能锁已重置'
								});
							}
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: '重置智能锁失败'
						});
					}
				});
		},
		getDetail() {
			api.lockDetail({ lock_id: this.id }).then((res) => {
				this.detail = res.data;
				uni.setNavigationBarTitle({
					title: this.detail.lock_alias
				});
			});
		}
	}
};
</script>
